package cc.rboot._core.helper;

import javax.servlet.http.HttpServletRequest;

public class OSHelper {

    public static final String REGEX_SPACE = " ";
    public static final String REGEX_SLASH = "/";

    private static final String HEADER_REWU = "rewu";

    public static final String PC = "PC".toLowerCase();
    public static final String MINI = "MINI".toLowerCase();
    public static final String ANDROID = "ANDROID".toLowerCase();
    public static final String IOS = "IOS".toLowerCase();

    public static boolean app() {
        return ios() || android();
    }

    public static boolean ios() {
        return os().equals(IOS);
    }

    public static boolean android() {
        return os().equals(ANDROID);
    }

    public static boolean mini() {
        return os().equals(MINI);
    }

    public static boolean pc() {
        return os().equals(PC);
    }

    public static String version() {
        return getValue("Version");
    }

    public static String productVersion() {
        return getValue("ProductVersion");
    }

    public static String os() {
        return getValue("OS").toLowerCase();
    }

    private static String getValue(String key) {
        HttpServletRequest request = RequestHelper.get();
        try {
            String rewu = request.getHeader(HEADER_REWU);
            String[] rewuList = rewu.split(REGEX_SPACE);
            for (String item : rewuList) {
                String[] itemMap = item.split(REGEX_SLASH);
                if (itemMap[0].equals(key)) {
                    return itemMap[1];
                }
            }
            return "";
        } catch (Exception ex) {
            return "";
        }
    }

}
